import {ctx} from './canvas.js'
const max_width = document.body.offsetWidth;
const max_height = document.documentElement.clientHeight;
//爆炸
function boom(item){
    this.width=item.width
    this.height=item.height
    this.left=item.left
    this.top=item.top
    this.animate=[
      document.getElementById('boom0'),
      document.getElementById('boom1'),
      document.getElementById('boom2'),
      document.getElementById('boom3'),
      document.getElementById('boom4'),
      document.getElementById('boom5'),
    ]
    this.animate_index=0
}
/**
 * 
 * @param {*} speed 
 * @param {*} degs 
 * 角度 0开始3点钟方向顺时针
 */
function mydeg(speed, degs) {
    let xspeed = Math.cos((degs * Math.PI) / 180) * speed; //返回都是负数
    let yspeed = Math.sin((degs * Math.PI) / 180) * speed;
    return [xspeed, yspeed];
  }
//根据两点坐标获取x轴,y轴的速度
function xyseed(item1,item2){
    let x =  item1.left +(item1.width / 2) -(item2.left + item2.width / 2);
    let y = item1.top +(item1.height / 2) -(item2.top + item2.height);
    let distance = Math.sqrt(x * x + y * y);
    x = (x / distance) ;
    y = (y / distance) ;
    return [x,y]
}
//碰撞检测
function hitBox(source, target) {
    /* 源物体和目标物体都包含 x, y 以及 width, height */
    return !(
      source.top + source.height < target.top ||
      source.top > target.top + target.height ||
      source.left + source.width < target.left ||
      source.left > target.left + target.width
    );
  }
//把图片分成像素组
function imgMap(image, resolution) {
var pixelMap = [];
for (var y = image.top; y < image.top+image.height; y = y + resolution) {
    for (var x = image.left; x < image.left+image.width; x = x + resolution) {
    // 获取当前位置的像素群
    var pixel = ctx.getImageData(x, y, resolution, resolution);

    // 判断像素群的透明度不为0
    if (pixel.data[3] !=0) {
        pixelMap.push({ left: x, top: y,height:resolution,width:resolution });
    }
    }
}

return {
    data: pixelMap,
    resolution: resolution,
};
}
//获取图片旋转的范围
function get_deg_rect(item){
  const w1=Math.cos(Math.PI/180*item.deg)*item.width
  const w2=Math.cos(Math.PI/180*(90-item.deg))*item.height
  const h1=Math.cos(Math.PI/180*item.deg)*item.height
  const h2=Math.cos(Math.PI/180*(90-item.deg))*item.width
  
  const width=w1+w2+'px'
  const height=h1+h2+'px'
  const left=item.left-width/2+item.width/2
  const top=item.top-height/2+item.height/2
  return {width,height,left,top}
}
/**+
 * 通过角度的距离获取位置 0从六点钟方向顺时针
 */
function get_deg_location(source,target,length,deg,speed){
  const left1=Math.round((target.left+target.width/2)+Math.sin(Math.PI/180*deg*-1)*length)
  const top1=Math.round((target.top+target.height/2)+Math.cos(Math.PI/180*deg*-1)*length)
  let x_speed=(target.left+target.width/2-left1)/length*speed
  let y_speed=(target.top+target.height/2-top1)/length*speed
  const left=left1-source.width
  const top=top1-source.height
  return {left,top,x_speed,y_speed}
}
//画图片
function drawImage(item){
  if(!item.deg){
    ctx.drawImage(item.dom,item.left,item.top,item.width,item.height)
  }else{
    ctx.save();
    ctx.translate(item.left+item.width/2,item.top+item.height/2);
    ctx.rotate(deg*Math.PI/180);
    ctx.drawImage(img,item.width/-2,item.height/-2);//把图片绘制在旋转的中心点，
	  ctx.restore();//恢复状态
  }
}
//检测对象超出画布
function out(item){

    if(item.top >= max_height ||item.left<=-200 || item.left>max_width+100 ||item.top<=-800){
      return true
    }else{
      return false
    }
  }
  //检测像素组是否碰撞
  function pixelHitTest(source, target) {
    // 源对象和目标对象包含两张属性
    // { data: a render-map, resolution: The precision of the render-map}
    return source.data.some(item => {
        return target.data.some(item1=>{
          if(hitBox(item,item1)){
            return true
          }
        })
      });
  }
  export{out,hitBox,xyseed,boom,pixelHitTest,imgMap,get_deg_location,drawImage,mydeg}